package com.hcbd.tdwt.ui.xingyu.fragment;

import android.annotation.SuppressLint;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;

import android.widget.EditText;
import android.widget.TextView;


import com.hcbd.tdwt.R;
import com.hcbd.tdwt.base.BaseFragment;
import com.hcbd.tdwt.base.BeidouBoxParams;

import com.pangu.bdsdk2021.entity.terminalone.BDMSHInfo;
import com.pangu.bdsdk2021.entity.terminalone.BDOKInfo;
import com.pangu.bdsdk2021.terminal.TerminalSendManager;


import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.text.MessageFormat;

import static java.lang.Thread.sleep;

@SuppressLint("NonConstantResourceId")
@EFragment(R.layout.fragment_device_report_peace)
public class DeviceReportPeaceFragment extends BaseFragment {
    @ViewById
    TextView frequencyTipsTV;
    @ViewById
    EditText safeContentET, centerNumber;
    @ViewById
    TextView startBtn;
    private String text = "当前北斗卡服务频度为:";
    Drawable ovaBlueDrawable;
    Drawable ovaGrayDrawable;
    Drawable ovaRedDrawable;
    private boolean isOpen = false;

    @AfterViews
    protected void init() {
        initMsgRegister();
        ovaBlueDrawable = getResources().getDrawable(R.drawable.ova_light_blue_bg);
        ovaRedDrawable = getResources().getDrawable(R.drawable.ova_red_bg2);
        ovaGrayDrawable = getResources().getDrawable(R.drawable.ova_gray_bg);
        frequencyTipsTV.setText(MessageFormat.format("{0}{1}", text, BeidouBoxParams.sendFreq));
        initData();

    }

    public void initData() {
        if (BeidouBoxParams.isBoxConnectNormal) {
            startBtn.setClickable(true);
            startBtn.setText("启动");
            startBtn.setBackground(ovaBlueDrawable);
            adapterHandler.sendEmptyMessage(2);
            //adapterHandler.sendEmptyMessageDelayed(1, 1000);
        } else {
            startBtn.setClickable(false);
            startBtn.setText("未连接！");
            startBtn.setBackground(ovaGrayDrawable);
        }
    }



    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true, priority = 2)
    public void eventBus(BDOKInfo bdokInfo){
        centerNumber.setText(String.valueOf(bdokInfo.centerNumber));
        safeContentET.setText(bdokInfo.content);
        adapterHandler.sendEmptyMessageDelayed(1, 500);
    }
    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true, priority = 2)
    public void eventBus(BDMSHInfo bdmshInfo){
        if (bdmshInfo.md == 8) {
            isOpen = true;
            startBtn.setClickable(true);
            startBtn.setText("关闭");
            startBtn.setBackground(ovaRedDrawable);
        } else {
            isOpen = false;
            startBtn.setClickable(true);
            startBtn.setText("启动");
            startBtn.setBackground(ovaBlueDrawable);
        }
    }

    @Click
    void startBtn() {
        String number = getTextStr(centerNumber);
        if ("".equals(number)) {
            toast("请输入中心号码!");
            return;
        }
        String content = getTextStr(safeContentET);
        if ("".equals(content)) {
            toast("请输入平安内容!");
            return;
        }
        showLoading("发送中");
        TerminalSendManager.getInstance().level_two_setOKInfo(Integer.parseInt(number), content);
        adapterHandler.sendEmptyMessageDelayed(3, 1000);
    }

    /**
     * 刷新UI
     */
    @SuppressLint("HandlerLeak")
    private Handler adapterHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    TerminalSendManager.getInstance().level_two_getCCMSC();
                    break;
                case 2:
                    TerminalSendManager.getInstance().level_two_getOKInfo();//获取OK信息
                    break;
                case 3:
                    if (isOpen) {
                        TerminalSendManager.getInstance().level_two_openOK(0);
                    } else {
                        TerminalSendManager.getInstance().level_two_openOK(1);
                    }

                    hideLoading();
                    break;
            }
        }
    };

    @Override
    public void onDestroyView() {
        super.onDestroyView();

    }

}
